package com.aaa.system.service.imp;

import com.aaa.entity.LoginInfo;
import com.aaa.system.mapper.LoginLogMapper;
import com.aaa.system.service.LoginLogService;
import com.aaa.system.vo.LoginInfoVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class LoginLogServiceImp implements LoginLogService {
    @Resource
    private LoginLogMapper loginLogMapper;

    @Override
    public Result<IPage<LoginInfo>> getAllLog(LoginInfoVo loginInfoVo) {
        IPage<LoginInfo> page = new Page<>(loginInfoVo.getCurrent(),loginInfoVo.getSize());
        QueryWrapper<LoginInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(loginInfoVo.getUserName())){
            wrapper.like("user_name",loginInfoVo.getUserName());
        }
        if (StringUtils.isNotBlank(loginInfoVo.getLoginAccount())){
            wrapper.like("login_account",loginInfoVo.getLoginAccount());
        }
        if (StringUtils.isNotBlank(loginInfoVo.getIpAddr())){
            wrapper.like("ip_addr",loginInfoVo.getIpAddr());
        }
        if (Objects.nonNull(loginInfoVo.getLoginStatus())){
            wrapper.eq("login_status",loginInfoVo.getLoginStatus());
        }
        if (Objects.nonNull(loginInfoVo.getLoginType())){
            wrapper.eq("login_type",loginInfoVo.getLoginType());
        }
        if (Objects.nonNull(loginInfoVo.getDateRange())&&loginInfoVo.getDateRange().length==2){
            wrapper.between("login_time",loginInfoVo.getDateRange()[0],loginInfoVo.getDateRange()[1]);
        }
        IPage<LoginInfo> page1 = loginLogMapper.selectPage(page, wrapper);
        return new Result<>(200,"查询成功",page1);
    }

    @Override
    public Result<Boolean> delLoginLog(Long[] infoIds) {
        int i = loginLogMapper.bachLoginLog(infoIds);
        return new Result<>(200,"删除成功",i>0?true:false);
    }

    @Override
    public Result<String> clearTable() {
        loginLogMapper.clearLog();
        return new Result<>(200,"清除成功");
    }

    @Override
    public void addLoginLog(LoginInfo loginInfo) {
        loginLogMapper.insert(loginInfo);
    }
}
